﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace JiveMessenger.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class LoginPage : BasicPage
    {
        public LoginPage()
        {
            this.InitializeComponent();
            Messenger.Default.Register<NotificationMessage>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "NavigateToMainPage":
                            if (msg.Sender == this.DataContext)
                                Frame.Navigate(typeof(MainPage));
                            break;
                    }
                });
            DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            await StatusBar.GetForCurrentView().HideAsync();
            base.OnNavigatedTo(e);

            Frame.BackStack.Clear();

            if (e.Parameter != null && e.Parameter is bool && (bool)e.Parameter == true)
            {
                ((ViewModelLoginControl)this.DataContext).Logout();
            }
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            ((ViewModelLoginControl)this.DataContext).Cleanup();
            base.OnNavigatedFrom(e);
            Frame.BackStack.Clear();
        }
    }
}
